<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
     append-to-body
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="开盘时间" prop="openingTime"
                :rules="[
                  {required: true, message:'开盘时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.openingTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="交房时间" prop="deliveryTime"
                :rules="[
                  {required: true, message:'交房时间不能为空', trigger:'blur'}
                 ]">
                <el-date-picker
                      style="width: 100%;"
                      v-model="inputForm.deliveryTime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="楼号" prop="buildingNum"
                :rules="[
                  {required: true, message:'楼号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.buildingNum" placeholder="请填写楼号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="单元" prop="unitNumber"
                :rules="[
                  {required: true, message:'单元不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.unitNumber" placeholder="请填写单元"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="层数" prop="layerNumber"
                :rules="[
                  {required: true, message:'层数不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.layerNumber" placeholder="请填写层数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="户数" prop="housesNumber"
                :rules="[
                  {required: true, message:'户数不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.housesNumber" placeholder="请填写户数"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="户型" prop="houseType.id"
                :rules="[
                  {required: true, message:'户型不能为空', trigger:'blur'}
                 ]">
                 <el-select v-model="inputForm.houseType.id" placeholder="请选择户型" size="small" style="width: 100%;">
                  <el-option v-for="(item,index) in houseTypeList"  :key="index" :label="item.title"
                    :value="item.id">
                  </el-option>
                </el-select>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          openingTime: '',
          deliveryTime: '',
          unitNumber: '',
          layerNumber: '',
          housesNumber: '',
          buildingsId:'',
          buildingNum:'',
          houseType: {
            id: ''
          }
        },
        houseTypeList:[]
      }
    },
    components: {
      GridSelect
    },
    methods: {
      init (method, id,buildingsId) {
        this.method = method
        this.inputForm.id = id
        this.inputForm.buildingsId = buildingsId
        this.houseTypeList = []
        console.log(this.inputForm.buildingsId);
        this.getHouseType();
        if (method === 'add') {
          this.title = `新建楼号设置`
        } else if (method === 'edit') {
          this.title = '修改楼号设置'
        } else if (method === 'view') {
          this.title = '查看楼号设置'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/buildingsnumber/tBuildingsNumber/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.tBuildingsNumber)
              this.loading = false
            })
          }
        })
      },
      //加载户型
      getHouseType(){
        this.$http({
              url: `/thouseinfo/tHouseType/list?buildings.id=${this.inputForm.buildingsId}`,
              method: 'get'
            }).then(({data}) => {
              this.houseTypeList =  data.page.list
            })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/buildingsnumber/tBuildingsNumber/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>